package com.whity.towerdefense.view;

import java.io.IOException;
import java.util.Vector;

import com.whity.towerdefense.R;
import com.whity.towerdefense.TowerDefenseActivity;
import com.whity.towerdefense.Utils;
import com.whity.towerdefense.model.GameModel;
import com.whity.towerdefense.model.Level;
import com.whity.towerdefense.model.TowerFactory;
import com.whity.towerdefense.model.TowerType;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.ViewStub.OnInflateListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
import android.widget.TextView;

public class ActionPhaseLayout extends FrameLayout
{
	// =============================================================
	// Private attributs
	
	// Constants
	private static final String TAG = "com.djm.towerdefense.view";
	
	// UI elements
	private ActionPhaseView		m_GameView;
	
	private SlidingDrawer		m_TowerDrawer;
	private Vector<Button>		m_TowerButtons;
	
	private TextView			m_OverlayLabel;
	
	// Current level
	private static Level		m_Level;
	
	// =============================================================
	// Public functions :
	public ActionPhaseLayout(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		m_Level = GameModel.LEVEL;
		m_TowerButtons = new Vector<Button>();
	}

	// =============================================================
	// Protected functions :
	
	protected void onFinishInflate()
	{
		// Get the overlay label
		m_OverlayLabel = (TextView)findViewById(R.id.StartWaveLabel);
		m_OverlayLabel.setOnClickListener(GameView.GAME_CONTROLER);
         
		// Get the sliding panel on right side
		m_TowerDrawer = (SlidingDrawer)findViewById(R.id.TowerSlider);
		m_TowerDrawer.setOnDrawerOpenListener(GameView.GAME_CONTROLER);
		m_TowerDrawer.setOnDrawerCloseListener(GameView.GAME_CONTROLER);
		m_TowerDrawer.setOnTouchListener(GameView.GAME_CONTROLER);
		
		//m_GameView = (ActionPhaseView)findViewById(R.id.Action_phase_view);
		//Log.w(TAG, "GameView " + m_GameView);
		//m_GameView.setOnTouchListener(GameView.GAME_CONTROLER);
		
		// Get the number of base tower in this level
		Vector<String> towerIDs = m_Level.GetBaseTowers();
		Vector<TowerType> baseTowers = new Vector<TowerType>();
		for(int i = 0 ; i < towerIDs.size() ; i++)
		{
			Button towerButton;
			if(i == 0)
			{
				towerButton = (Button)findViewById(R.id.Tower01);
			}
			else if(i == 1)
			{
				towerButton = (Button)findViewById(R.id.Tower02);
			}
			else if(i == 2)
			{
				towerButton = (Button)findViewById(R.id.Tower03);
			}
			else if(i == 3)
			{
				towerButton = (Button)findViewById(R.id.Tower04);
			}
			else
			{
				continue;
			}
			towerButton.setOnClickListener(GameView.GAME_CONTROLER);
			towerButton.setOnTouchListener(GameView.GAME_CONTROLER);
			// Get the tower type
			TowerType towerType = TowerFactory.GetTowerType(towerIDs.elementAt(i));
			// Get the button ready to be used by the user
			towerButton.setVisibility(VISIBLE);
			towerButton.setText("");
			
			BitmapDrawable image;
			try
			{
				image = new BitmapDrawable(GameView.ASSETS_MANAGER.open(Utils.GRAPHICS_PATH + towerType.GetGraphicHeartPath() + "/button.png"));
				towerButton.setBackgroundDrawable(image);
			}
			catch (IOException e)
			{
				try
				{
					image = new BitmapDrawable(GameView.ASSETS_MANAGER.open(Utils.PANIC_IMAGE));
					towerButton.setBackgroundDrawable(image);
				}
				catch (IOException e1)
				{
					Log.e(TAG, "Can't load the panic display image : " + Utils.PANIC_IMAGE + "\nerror : " + e.toString());
					System.exit(Utils.CANT_LOAD_PANIC_IMAGE);
				}
				
				Log.w(TAG, "No button.png image present here :" + Utils.GRAPHICS_PATH + towerType.GetGraphicHeartPath() + "/button.png" + ", PANIC image was used");
			}
			
			//TODO name, background etc
			
			m_TowerButtons.add(towerButton);
			
		}
	}
	
	// =============================================================
}
